// pages/wddd/wddd.js
import instance from "../../utils/util"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cur: 0,
        //'pending','paid','shipped','delivered','completed','cancelled','refunded'
        status:"pending",
        keyword:"",
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getcollect()
    },

    async getcollect() {
        console.log(this.data.cur)
        let data = JSON.parse(wx.getStorageSync('info'))
        if(this.data.cur !==3){
            const ress = await instance.get("/api/orders?userId=" + data.id+"&&status="+this.data.status);
            console.log(ress)
            this.setData({
                collect: ress.data.list
            })
    
        }else{

            const ress = await instance.get("/api/orders?userId=" + data.id+"&&status="+this.data.status);
        console.log(ress)
const resss=await instance.get("/api/order-reviews");
console.log(resss)
let data1=ress.data.list;
data1.forEach(el => {
 resss.data.list.forEach(ell=>{
 
     if(el.id == ell.orderId){
        console.log(ell)
        el.pj=ell.content
         el.reply=ell.reply
     }
 })   
});
console.log(data1)
        this.setData({
            collect: data1
        })

        }
        
        
    },
    clickTab(e){
        console.log(e)
        this.setData({
            cur:Number(e.currentTarget.dataset.current)
        })
        if(this.data.cur == 1){
            this.setData({
                status:"shipped"
            })
            this.getcollect()
        }
        if(this.data.cur == 0){
            this.setData({
                status:"pending"
            })
            this.getcollect()
            
        }
        if(this.data.cur == 2){
            this.setData({
                status:"delivered"
            })
            this.getcollect()
            
        }
        if(this.data.cur == 3){
            this.setData({
                status:"completed"
            })
            this.getcollect()
            
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    fanhui(){
        wx.navigateBack()
    },
    pj(e){
        console.log(e)
        wx.navigateTo({
          url: '/pages/pj/pj?id='+e.currentTarget.dataset.id,
        })
    },
   async cancel(e){
        console.log(e)
        const res=await instance.put(`/api/orders/${e.currentTarget.dataset.id}/status`,{
            status:"cancelled"
        })
        this.getcollect()
    },
  async  qrsh(e){
        console.log(e)
        const res=await instance.put(`/api/orders/${e.currentTarget.dataset.id}/status`,{
            status:"delivered"
        })
        wx.showToast({
          title: '收货成功',
          icon:false
        })
        this.getcollect()
    },
    searchdd(e){
        console.log(e)
    this.setData({
        keyword:e.detail.value
    })
    },
    con(){

    },
    zf(e){
        wx.navigateTo({
          url: '/pages/zf/zf?id='+e.currentTarget.dataset.id,
        })
    },
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})